/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xume.raob.statements;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.xume.raob.ObjectBuilder;
import org.xume.raob.annotations.PropertyStatementsFactory;
import org.xume.raob.exceptions.ObjectBuilderConfigurationException;
import org.xume.raob.util.ReflectionUtils;

/**
 * @author Johan Siebens
 */
@SuppressWarnings("unchecked")
public class StatementsFactoryIntrospectionResult {

	private static final Map<Class<? extends ObjectBuilder<?>>, StatementsFactoryIntrospectionResult> CACHE = new HashMap<Class<? extends ObjectBuilder<?>>, StatementsFactoryIntrospectionResult>();

	public static StatementsFactoryIntrospectionResult forBuilder(Class<? extends ObjectBuilder<?>> builderInterface) {
		StatementsFactoryIntrospectionResult result = CACHE.get(builderInterface);
		if (result == null) {
			result = new StatementsFactoryIntrospectionResult();
			Class<?> targetClass = ReflectionUtils.targetClass(builderInterface);
			Method[] methods = builderInterface.getDeclaredMethods();
			for (Method method : methods) {
				if (builderInterface.isAssignableFrom(method.getReturnType())) {
					Annotation[] annotations = method.getAnnotations();
					for (Annotation annotation : annotations) {
						Class<? extends Annotation> annotationType = annotation.annotationType();
						StatementsFactoryClass factoryClassAnnotation = annotationType.getAnnotation(StatementsFactoryClass.class);
						if (factoryClassAnnotation != null) {
							StatementsFactory statementsFactory = ReflectionUtils.createInstance(factoryClassAnnotation.value());
							statementsFactory.initialize(annotation, method, targetClass);
							result.statementsFactories.put(method, statementsFactory);
						} else {
							PropertyStatementsFactory statementsFactory = new PropertyStatementsFactory();
							statementsFactory.initialize(method, targetClass);
							result.statementsFactories.put(method, statementsFactory);
						}
					}
				} else {
					throw new ObjectBuilderConfigurationException(
							"Methods declared in ObjectBuilders should return an instance of this ObjectBuilder");
				}
			}
			CACHE.put(builderInterface, result);
		}
		return result;
	}

	private Map<Method, StatementsFactory<?>> statementsFactories;

	private StatementsFactoryIntrospectionResult() {
		this.statementsFactories = new HashMap<Method, StatementsFactory<?>>();
	}

	public boolean contains(Method method) {
		return statementsFactories.containsKey(method);
	}

	public StatementsFactory<?> get(Method method) {
		return statementsFactories.get(method);
	}

}
